package betAndWin.gui;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;

import javax.swing.JComponent;
import javax.swing.JPanel;

public class TVComponent extends JComponent {

	private static final String IMG_PATH = "img/tv.png";
	
	private static final int PREFERRED_WIDTH = 150;
	private static final int PREFERRED_HEIGHT = 150;
	
	private static final int IMG_START_OFFSET_X = 234;
	private static final int IMG_START_OFFSET_Y = 185;
	private static final int IMG_END_OFFSET_X = 235;
	private static final int IMG_END_OFFSET_Y = 284;

	private Image img;
	private JPanel tvContentPanel;

	public TVComponent() {
		setOpaque(false);

		Toolkit tk = Toolkit.getDefaultToolkit();
		img = tk.getImage(IMG_PATH);
		MediaTracker mt = new MediaTracker(this);    //Attende che il caricamento sia stato completato
		mt.addImage(img, 1);

		tvContentPanel = new JPanel();
		this.add(tvContentPanel);
		
		setPreferredSize(new Dimension(PREFERRED_WIDTH, PREFERRED_HEIGHT));

		try {
			mt.waitForAll();
		}
		catch (InterruptedException e){
		}
	}
	
	public JPanel getTVContentPanel() {
		return this.tvContentPanel;
	}

	protected void paintComponent(Graphics g){
		super.paintComponent(g);

		int width = getWidth();
		int height = getHeight();
		int imgWidth = img.getWidth(null);
		int imgHeight = img.getHeight(null);
		int contentPanelX = IMG_START_OFFSET_X * width / imgWidth;
		int contentPanelY = IMG_START_OFFSET_Y * height / imgHeight;
		int contentPanelWidth = width - ((IMG_START_OFFSET_X + IMG_END_OFFSET_X) * width / imgWidth);
		int contentPanelHeight = height - ((IMG_START_OFFSET_Y + IMG_END_OFFSET_Y) * height / imgHeight);

		g.drawImage(img, 0, 0, width, height, null);
		tvContentPanel.setBounds(contentPanelX, contentPanelY, contentPanelWidth, contentPanelHeight);
	}
}
